<?php

namespace App\ModelFilters;

use EloquentFilter\ModelFilter;

class BaseFilter extends ModelFilter
{
    // company => company()
    protected $drop_id = false;
    // mobile_phone => mobile_phone()
    protected $camel_cased_methods = false;

    public function params($params)
    {
        if ($params['beginTime'] && $params['endTime']) {
            if (strlen($params['beginTime']) == 10) {
                $params['beginTime'] = $params['beginTime'] . " 00:00:00";
            }
            if (strlen($params['endTime']) == 10) {
                $params['endTime'] = $params['endTime'] . " 23:59:59";
            }
            return $this->whereBetween('created_at', [strtotime($params['beginTime']), strtotime($params['endTime'])]);
        }
        return $this;
    }
    public function updated_at($params)
    {
        if ($params['beginTime'] && $params['endTime']) {
            if (strlen($params['beginTime']) == 10) {
                $params['beginTime'] = $params['beginTime'] . " 00:00:00";
            }
            if (strlen($params['endTime']) == 10) {
                $params['endTime'] = $params['endTime'] . " 23:59:59";
            }
            return $this->whereBetween('updated_at', [strtotime($params['beginTime']), strtotime($params['endTime'])]);
        }
        return $this;
    }

    protected function filterEmptyParam($param)
    {

        foreach ($param as $key => $value) {
            if ($value !== '' && $value !== null && !(is_array($value) && empty($value))) {
                $param[$key] = trim($value);
            } else {
                unset($param[$key]);
            }
        }
        return $param;
    }
}
